﻿var defaultDelayTime = 3500;
var defaultFontSize = 30;

function getTitle(xmlDocResponse){
    var title = xmlDocResponse.getElementsByTagName("title")[0].childNodes[0].nodeValue;
    return title;
}

function getLines(xmlDocResponse) {
    var l = xmlDocResponse.getElementsByTagName("line");
    var imageFinal = new Array();
    for (j = 0; j < l.length; j++) {
        var lineOrderImage = new Array();
        lineOrderImage[0] = l[j].getAttribute("order");
        var image = l[j].getAttribute("image");
        var delayTime;
        var fontSize;
        if (l[j].getAttribute("speed")) {
            delayTime = l[j].getAttribute("speed");
        }
        else {
            delayTime = defaultDelayTime;
        }

        if (l[j].getAttribute("fontsize")) {
            fontSize = l[j].getAttribute("fontsize");
        }
        else {
            fontSize = defaultFontSize;
        }
        var caption = l[j].childNodes[0].nodeValue;
        lineOrderImage[1] = assembleScene(image, caption, fontSize);
        lineOrderImage[2] = delayTime;
        imageFinal.push(lineOrderImage);
    }
    return imageFinal;
}

function assembleScene(imgURL, line, fontSize) {
    var callURL = "http://cheezburger.com/caption/previewcaption.ashx?%3c%3fxml+version%3d%221.0%22+encoding%3d%22utf-8%22%3f%3e%3cCaptionData%3e%3cOriginalImageUrl%3e" + encodeURIComponent(imgURL) + "%3c%2fOriginalImageUrl%3e%3cCaptions%3e%3cCaption%3e%3cText%3e" + encodeURIComponent(line) + "%3c%2fText%3e%3cFontSize%3e" + fontSize + "%3c%2fFontSize%3e%3cXPosition%3e10%3c%2fXPosition%3e%3cYPosition%3e295%3c%2fYPosition%3e%3c%2fCaption%3e%3c%2fCaptions%3e%3c%2fCaptionData%3e";
    return callURL;
}

